package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/demo01")
public class ServletDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();

        List<Contact> list = new ArrayList<>();
        list.add(new Contact(1, "张三", "男", 18));
        list.add(new Contact(2, "李四", "女", 22));
        list.add(new Contact(3, "王五", "男", 28));

        writer.println("<table border=\"1\" cellspacing=\"0\" width=\"80%\">");
        writer.println("<tr>");
        writer.println("<th>序号</th>");
        writer.println("<th>姓名</th>");
        writer.println("<th>性别</th>");
        writer.println("<th>年龄</th>");
        writer.println("</tr>");

        for (Contact contact : list) {
            writer.println("<tr>");
            writer.println("<td>" + contact.getId() + "</td>");
            writer.println("<td>" + contact.getName() + "</td>");
            writer.println("<td>" + contact.getSex() + "</td>");
            writer.println("<td>" + contact.getAge() + "</td>");
            writer.println("</tr>");
        }

        writer.println("</table>");
    }
}

class Contact {
    private int id;
    private String name;
    private String sex;
    private int age;

    public Contact() {
    }

    public Contact(int id, String name, String sex, int age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Contact{id = " + id + ", name = " + name + ", sex = " + sex + ", age = " + age + "}";
    }
}